package com.example.micro.netty.server.thread;

import lombok.SneakyThrows;

import java.util.concurrent.*;

/**
 * 信号量demo
 */
public class SemaphoreDemo implements Runnable {

    final Semaphore semaphore = new Semaphore(5);

    @SneakyThrows
    @Override
    public void run() {
        semaphore.acquire();
//        semaphore.acquireUninterruptibly();
        Thread.sleep(2000);
        System.out.println(Thread.currentThread().getId() + "done");
        semaphore.release();
    }

    public static void main(String[] args) {
        try{
            ExecutorService executorService = Executors.newFixedThreadPool(20);
            SemaphoreDemo semaphoreDemo = new SemaphoreDemo();
            for (int i = 0; i < 20; i++) {
                executorService.submit(semaphoreDemo);
            }
        }catch (Exception ex){

        }
    }
}
